package org.ysh.vo;

import lombok.Data;
import org.ysh.domain.Resource;
import org.ysh.domain.RoleResource;

import java.util.ArrayList;
import java.util.List;

@Data
public class LayuiTreeNodeVo {

    private Integer id;

    private String title;

    private String field;

    private Boolean spread = false;

    private Boolean checked = false;

    private List<LayuiTreeNodeVo> children;

    public LayuiTreeNodeVo(){}

    public static List<LayuiTreeNodeVo> buildTree(String parentCode,List<Resource> resources,List<RoleResource> roleResources){
        List<LayuiTreeNodeVo> layuiTreeNodeVos = new ArrayList<>();
        if(resources==null || resources.isEmpty()){
            return layuiTreeNodeVos;
        }
        layuiTreeNodeVos = findChildren(parentCode,resources);

        //设置选中节点
        setChecked(layuiTreeNodeVos,roleResources);

        return layuiTreeNodeVos;
    }

    public static List<LayuiTreeNodeVo> buildTree(List<Resource> resources,List<RoleResource> roleResources){
        return buildTree("",resources,roleResources);
    }

    public static List<LayuiTreeNodeVo> buildTree(List<Resource> resources){
        return buildTree(resources,null);
    }

    private static List<LayuiTreeNodeVo> findChildren(String parentCode, List<Resource> resources) {
        List<LayuiTreeNodeVo> children = new ArrayList<>();
        for(Resource resource : resources){
            if(resource.getParentCode().equals(parentCode)){
                LayuiTreeNodeVo layuiTreeNodeVo = new LayuiTreeNodeVo();
                layuiTreeNodeVo.id = resource.getId();
                layuiTreeNodeVo.title = resource.getResourceName();
                layuiTreeNodeVo.field = resource.getResourceCode();
                children.add(layuiTreeNodeVo);
                //递归查找子节点
                layuiTreeNodeVo.setChildren(findChildren(resource.getResourceCode(), resources));
            }
        }
        return children.isEmpty()? null : children;
    }

    // 设置选中
    private static void setChecked(List<LayuiTreeNodeVo> layuiTreeNodeVos,List<RoleResource> roleResources){
        if(layuiTreeNodeVos==null || layuiTreeNodeVos.isEmpty()){
            return;
        }
        for (LayuiTreeNodeVo layuiTreeNodeVo : layuiTreeNodeVos) {
            if(layuiTreeNodeVo.children != null &&!layuiTreeNodeVo.children.isEmpty()) {
                // 递归查找子节点，有子节点，继续递归，没有子节点，设置选中状态 https://blog.csdn.net/qq_37496750/article/details/115859243?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ECtr-1-115859243-blog-111659769.235%5Ev43%5Epc_blog_bottom_relevance_base4&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ECtr-1-115859243-blog-111659769.235%5Ev43%5Epc_blog_bottom_relevance_base4&utm_relevant_index=2
                setChecked(layuiTreeNodeVo.getChildren(),roleResources);
                continue;
            }
            for (RoleResource roleResource : roleResources) {
                if(layuiTreeNodeVo.getId().equals(roleResource.getResourceId())){
                    layuiTreeNodeVo.setChecked(true);
                    break;
                }
            }
        }
    }


}
